package com.bdqn.zhang.dao;

import com.bdqn.zhang.entity.RealEstate;
import com.bdqn.zhang.entity.SysUser;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.springframework.orm.hibernate5.support.HibernateDaoSupport;
import org.springframework.orm.hibernate5.HibernateCallback;

import java.util.List;

/**
 * Created by 任 on 2019/4/11.
 */
public class RealEstateDaoImpl extends HibernateDaoSupport implements RealEstateDao {


    @Override
    public List<RealEstate> findByestate(RealEstate estate) {

        List<RealEstate> restateList = (List<RealEstate>) this.getHibernateTemplate().findByValueBean("from REAL_ESTATE", estate);
        if (restateList.size() > 0 && restateList != null) {
            return restateList;
        } else {
            return null;
        }
    }

    /**
     * 查询
     * @param page
     * @param limit
     * @return
     */
    @Override
    public List<RealEstate> findAllRealEstate(final Integer page, final Integer limit) {
        return this.getHibernateTemplate().execute(
                new HibernateCallback<List<RealEstate>>() {
                    @Override
                    public List<RealEstate> doInHibernate(Session session) throws HibernateException {
                        return  session.createQuery("from RealEstate r left join fetch r.users order by id asc").setFirstResult((page-1)*limit).setMaxResults(limit).list();


                    }
                });
    }

    /**
     * 删除
     * @param realEstate
     * @return
     */
    @Override
    public boolean delRealEstate(RealEstate realEstate) {
       this.getHibernateTemplate().delete(realEstate);
        return true;
    }

    /**
     * 修改
     * @param realEstate
     * @return
     */
    @Override
    public boolean updRealEstate(RealEstate realEstate) {
        this.getHibernateTemplate().update(realEstate);
        return true;
    }

    /**
     * 添加
     * @param realEstate
     * @return
     */
    @Override
    public boolean addRealEstate(RealEstate realEstate) {
        if(this.getHibernateTemplate().save(realEstate)!=null){
            return true;
        };
        return false;

    }
}
